package com.dmatek.uwb.xml.container;

import java.util.List;
import java.util.Map;

import org.dom4j.Element;

import com.dmatek.uwb.util.tool.StringUtil;
import com.dmatek.uwb.xml.util.iparse.IXmlElementParse;
/***
 *   xml容器，用于放置XML解析后的对象
 *   解析的顺序是由上向下解析
 * @author zf
 * @data 2019年6月13日 下午3:54:40
 * @Description
 */
public class XmlContainer extends BaseXmlContainer {
	private IXmlElementParse<?>[] domParses;
	public XmlContainer(IXmlElementParse<?>...domParses) {
		super();
		this.domParses = domParses;
	}
	@Override
	public Map<String, Object> parse(String xmlPath) throws Exception {
		Element root = obtainRoot(xmlPath);
		List<Element> domElements = obtainDomElements(root);
		List<Element> mapElements = obtainMapElements(root);
		List<Element> listElements= obtainListElements(root);
		// 先读取所有的根元素的孩子节点
		scanElements(domElements);
		// 扫描xml上的list节点
		scanElements(listElements);
		// 扫描xml上的map节点
		scanElements(mapElements);
		return getXmlContainer();
	}
	/***
	 * 匹配指定类的DomElement并生成对象添加到容器中
	 * @param key
	 * @param clazzName
	 * @param element
	 * @throws Exception
	 */
	private void matchDomElements(String key, String clazzName, Element element) throws Exception {
		for (IXmlElementParse<?> domParse : getDomParses()) {
			if(domParse.isMatch(clazzName)) {
				Object domElement = domParse.parse(element, getXmlContainer());
				if(null != domElement) {
					getXmlContainer().put(key, domElement);
					return;
				}
			}
		}
	}
	/***
	 * 匹配maps类的元素
	 * @param key
	 * @param mapName
	 * @param element
	 * @throws Exception
	 */
	private void matchMapElements(String key, String mapName, Element element) throws Exception {
		for (IXmlElementParse<?> domParse : getDomParses()) {
			if(domParse.isMatch(key)) {
				Object domElement = null;
				domElement = domParse.parse(element, getXmlContainer());
				if(null != domElement) {
					getXmlContainer().put(key, domElement);
					return;
				}
			}
		}
	}
	/***
	 * 匹配指定类的ListElement并生成对象添加到容器中
	 * @param key
	 * @param ListName
	 * @param element
	 * @throws Exception
	 */
	private void matchListElements(String key, String ListName, Element element) throws Exception {
		for (IXmlElementParse<?> domParse : getDomParses()) {
			// List类型, 需要我们将他们解析成List对象
			if(domParse.isMatch("dividerList")) {
				Object domElement = null;
				domElement = domParse.parse(element, getXmlContainer());
				if(null != domElement) {
					getXmlContainer().put(key, domElement);
					return;
				}
			}
		}
	}
	/***
	 * 扫描根节点的Element节点,并生成Element对象添加到容器中
	 * @param domElements
	 * @throws Exception
	 */
	private void scanElements(List<Element> mapElements) throws Exception {
		for (Element element : mapElements) {
			String clazzName = element.attributeValue("class");
			String id = element.attributeValue("id");
			if("map".equalsIgnoreCase(clazzName)) {
				// 扫描Map元素
				matchMapElements(StringUtil.isEmpty(id)?clazzName:id, clazzName, element);
			} else if("dividerList".equalsIgnoreCase(clazzName)) {
				// 扫描List元素
				matchListElements(StringUtil.isEmpty(id)?clazzName:id, clazzName, element);
			} else {
				// 扫描Dom元素
				matchDomElements(StringUtil.isEmpty(id)?clazzName:id, clazzName, element);
			}
		}
	}
	public IXmlElementParse<?>[] getDomParses() {
		return domParses;
	}
	public void setDomParses(IXmlElementParse<?>[] domParses) {
		this.domParses = domParses;
	}
}
